﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.FamingWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.FamingWay
{
    public class AddFamingWayCommandHandler : IRequestHandler<AddFamingWayCommand, ApiResult<int>>
    {
        IBaseReponsitory<Farmings> Farmings;

        public AddFamingWayCommandHandler(IBaseReponsitory<Farmings> Farmings)
        {
            this.Farmings = Farmings;
        }

        public async Task<ApiResult<int>> Handle(AddFamingWayCommand request, CancellationToken cancellationToken)
        {
            if (request.Farmings.Farming_Type == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "品种不能为空",
                };
            }
            if (request.Farmings.Farming_Way == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "来源不能为空",
                };
            }
            if (request.Farmings.Farming_Days == 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "饲养天数不能为空",
                };
            }
            if (request.Farmings.Farming_FoodType == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "饲养品牌不能为空",
                };
            }
            if (request.Farmings.Farming_Fooder == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "负责人不能为空",
                };
            }
            if (request.Farmings.Farming_OutintoTime == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "出栏日期不能为空",
                };
            }



            await Farmings.AddAsync(request.Farmings);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "添加成功",
                Data = 1
            };
        }
    }
}
